package com.wframe.framework.util;

import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.wframe.framework.spring.SpringContextHolder;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;


public class RedisUtil {

	private static Logger logger = LoggerFactory.getLogger(RedisUtil.class);
	private static ShardedJedisPool pool=null;
	
	static{
		pool=(ShardedJedisPool)SpringContextHolder.getBean("shardedJedisPool");
	}
	
	public static ShardedJedis getRedisClient() {
		try {  
            ShardedJedis shardJedis = pool.getResource();  
            return shardJedis;  
        } catch (Exception e) {  
            logger.error("getRedisClent error", e);  
        }  
        return null;  
    } 
	
	/** 
     * 设置单个值 
     *  
     * @param key 
     * @param value 
     * @return 
     */  
    public static String set(String key, String value) {  
        String result = null;  
  
        ShardedJedis shardedJedis = getRedisClient();  
        if (shardedJedis == null) {  
            return result;  
        }  
        boolean broken = false;  
        try {  
            result = shardedJedis.set(key, value);  
        } catch (Exception e) {  
            logger.error(e.getMessage(), e);  
            broken = true;  
        } finally {  
        	returnResource(shardedJedis, broken);  
        }  
        return result;  
    }
    
    /** 
     * 获取单个值 
     *  
     * @param key 
     * @return 
     */  
    public static String get(String key) {  
        String result = null;  
        ShardedJedis shardedJedis = getRedisClient();  
        if (shardedJedis == null) {  
            return result;  
        }  
        boolean broken = false;  
        try {  
            result = shardedJedis.get(key);  
  
        } catch (Exception e) {  
            logger.error(e.getMessage(), e);  
            broken = true;  
        } finally {  
            returnResource(shardedJedis, broken);  
        }  
        return result;  
    }
    
    public static Boolean exists(String key) {  
        Boolean result = false;  
        ShardedJedis shardedJedis = getRedisClient();  
        if (shardedJedis == null) {  
            return result;  
        }  
        boolean broken = false;  
        try {  
            result = shardedJedis.exists(key);  
        } catch (Exception e) {  
            logger.error(e.getMessage(), e);  
            broken = true;  
        } finally {  
            returnResource(shardedJedis, broken);  
        }  
        return result;  
    } 
    
    public static boolean setbit(String key, long offset, boolean value) {  
    	  
        ShardedJedis shardedJedis = getRedisClient();  
        boolean result = false;  
        if (shardedJedis == null) {  
            return result;  
        }  
        boolean broken = false;  
        try {
            result = shardedJedis.setbit(key, offset, value);  
        } catch (Exception e) {  
            logger.error(e.getMessage(), e);  
            broken = true;  
        } finally {  
            returnResource(shardedJedis, broken);  
        }  
        return result;  
    }  
  
    public static boolean getbit(String key, long offset) {  
        ShardedJedis shardedJedis = getRedisClient();  
        boolean result = false;  
        if (shardedJedis == null) {  
            return result;  
        }  
        boolean broken = false;  
  
        try {  
            result = shardedJedis.getbit(key, offset);  
        } catch (Exception e) {  
            logger.error(e.getMessage(), e);  
            broken = true;  
        } finally {  
            returnResource(shardedJedis, broken);  
        }  
        return result;  
    } 
    
    public static Long hset(String key, String field, String value) {  
        Long result = null;  
        ShardedJedis shardedJedis =getRedisClient();  
        if (shardedJedis == null) {  
            return result;  
        }  
        boolean broken = false;  
        try {  
            result = shardedJedis.hset(key, field, value);  
  
        } catch (Exception e) {  
            logger.error(e.getMessage(), e);  
            broken = true;  
        } finally {  
            returnResource(shardedJedis, broken);  
        }  
        return result;  
    }  
  
    public static String hget(String key, String field) {  
        String result = null;  
        ShardedJedis shardedJedis = getRedisClient();  
        if (shardedJedis == null) {  
            return result;  
        }  
        boolean broken = false;  
        try {  
            result = shardedJedis.hget(key, field);  
  
        } catch (Exception e) {  
            logger.error(e.getMessage(), e);  
            broken = true;  
        } finally {  
            returnResource(shardedJedis, broken);  
        }  
        return result;  
    }  
  
    public static Long hsetnx(String key, String field, String value) {  
        Long result = null;  
        ShardedJedis shardedJedis =getRedisClient();  
        if (shardedJedis == null) {  
            return result;  
        }  
        boolean broken = false;  
        try {  
            result = shardedJedis.hsetnx(key, field, value);  
  
        } catch (Exception e) {  
            logger.error(e.getMessage(), e);  
            broken = true;  
        } finally {  
            returnResource(shardedJedis, broken);  
        }  
        return result;  
    }  
  
    public static String hmset(String key, Map<String, String> hash) {  
        String result = null;  
        ShardedJedis shardedJedis = getRedisClient();  
        if (shardedJedis == null) {  
            return result;  
        }  
        boolean broken = false;  
        try {  
            result = shardedJedis.hmset(key, hash);  
  
        } catch (Exception e) {  
            logger.error(e.getMessage(), e);  
            broken = true;  
        } finally {  
            returnResource(shardedJedis, broken);  
        }  
        return result;  
    }  
  
    public List<String> hmget(String key, String... fields) {  
        List<String> result = null;  
        ShardedJedis shardedJedis = getRedisClient();  
        if (shardedJedis == null) {  
            return result;  
        }  
        boolean broken = false;  
        try {  
            result = shardedJedis.hmget(key, fields);  
  
        } catch (Exception e) {  
            logger.error(e.getMessage(), e);  
            broken = true;  
        } finally {  
            returnResource(shardedJedis, broken);  
        }  
        return result;  
    }
    
    public static void returnResource(ShardedJedis shardedJedis) {  
        pool.returnResource(shardedJedis);  
    }
    public static void returnResource(ShardedJedis shardedJedis, boolean broken) {  
        if (broken) {  
            pool.returnBrokenResource(shardedJedis);  
        } else {  
            pool.returnResource(shardedJedis);  
        }  
    }
}
